package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: ZHl
 * @Date: 2022-09-11 14:42
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "员工控制器")  //swagger API
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询所有员工
     * @return  AjaxResult
     */
    @GetMapping
    public AjaxResult list(){
       return AjaxResult.createSuccess( employeeService.findAll());
    }


    /**
     * 查询所有员工(分页)
     * @return  AjaxResult
     */
    @PostMapping("/pageList")
    public PageList findPage(@RequestBody EmployeeQuery employeeQuery){
        PageList pageResult = employeeService.pageList(employeeQuery);
        return pageResult;
    }

    /**
     * 查询所有部门(分页)
     * @return  AjaxResult
     */
    @GetMapping("/findAllManager")
    public AjaxResult findAllManager(){
        List<Employee> employees =  employeeService.findAllManager();
        return AjaxResult.createSuccess( employees);
    }



    /**
     * 新增/修改员工
     * @return AjaxResult
     */
    @PostMapping
    public AjaxResult saveOrUpdate(@RequestBody  Employee employee){

            if(employee.getId() == null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.createSuccess();
    }

    /**
     * 删除员工
     * @return AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable  Long id){
        employeeService.delete(id);
        return AjaxResult.createSuccess();
    }

    /**
     * 批量删除员工
     * @return AjaxResult
     */
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody EmployeeQuery employeeQuery){
        employeeService.deleteBatch(employeeQuery);
        return AjaxResult.createSuccess();
    }

}
